<?php 
/**
 * minf - core library
 * modules/string.php
 *
 * @author per function || minf.org team
 * @version 0.beta4
 * @copyright no,no,left, minf.org, 25 February, 2009
 * @license GPL
 * @package default
 **/

/**
 * Checks for if $str ends with $tomatch
 * ! this function is case-sensitive
 *
 * @param String $str Subject
 * @param String $tomatch Ending String
 * @return Boolean Match
 */
function str_matchEnding($str, $tomatch, $trim=false) {
	if ($trim) {
		$str = trim($str);
		$tomatch = trim($tomatch);
	}
	return (substr($str, -strlen($tomatch))==$tomatch);
}

/**
 * Checks for if $str starts with $tomatch
 * ! this function is case-sensitive
 *
 * @param String $str Subject
 * @param String $tomatch Ending String
 * @return Boolean Match
 */
function str_matchStart($str, $tomatch, $trim=false, $lowercase = false) {
	if ($trim) {
		$str = trim($str);
		$tomatch = trim($tomatch);
	}
	if ($lowercase) {
		$str = strtolower($str);
		$tomatch = strtolower($tomatch);
	}
	if ($str==$tomatch) return 1007; // PERFECT MATCH
	 
	$matchlen = ( strlen($str) > strlen($tomatch) ) ? strlen($tomatch) : strlen($str);

	for ($i=$matchlen;$i>0;$i--)
		if (substr($tomatch, 0, $i)==substr($str,0,$i)) return $i;
	return $false;
}

/**
 * Explodes and trims every pieces
 * 
 * @param String $delimiter
 * @param String $str
 * @return array
 */
function explodetrim($delimiter, $str) {
	$ret = array();
	foreach (explode($delimiter, $str) as $s) $ret[] = trim($s);
	return $ret;
}

function _parserelaxarray($array) {
	if (is_array($array)) return $array;
	if (strlen($array)>0) return explodetrim(',', $array);
}

/**
 * 
 *
 */
function print_rr($var) {
	echo '<pre>';
	print_r($var);
	echo '</pre>';
}

function parse_relaxarray($RelaxArray, $PartnerArray = null) {	
	$RelaxArray = _parserelaxarray($RelaxArray);
	
	if ($PartnerArray==null) return $RelaxArray;
	
	if (!is_array($PartnerArray)) {
		$PartnerArray = _parserelaxarray($PartnerArray);
		return array_combine($RelaxArray, $PartnerArray);
	}else{
		$Result = array();
		foreach ($RelaxArray as $_key => $_val)
			$Result[$_val] = $PartnerArray[$_val];
		return $Result; 
	}
}

function str_popline(&$str, $delimiter = "\n") {
	$pieces = explode($delimiter, $str);
	$line = array_pop($pieces);
	$str = implode($delimiter, $pieces);
	return $line;
}

function str_pushline ($line, &$str, $delimiter = "\n") {
	$pieces = explode($delimiter, $str);
	array_push($pieces, $line);
	$str = implode($delimiter, $pieces);
	return true;	
}

?>